package com.dating.core;

import org.apache.shiro.util.ByteSource;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.Ioc;
import org.nutz.lang.Lang;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import com.dating.base.bean.Permission;
import com.dating.base.bean.Role;
import com.dating.base.bean.RolePermission;
import com.dating.base.bean.User;
import com.dating.base.bean.User.Status;
import com.dating.base.bean.UserRole;
import com.dating.base.dao.PermissionDao;
import com.dating.base.dao.RoleDao;
import com.dating.base.dao.RolePermissionDao;
import com.dating.base.dao.UserDao;
import com.dating.base.dao.UserRoleDao;
import com.dating.base.vo.InstalledPermission;
import com.dating.base.vo.InstalledRole;

/**
 * 
 * 初始化，核心表结构，核心权限数据
 * 
 * @author tovish
 *
 */
public class MainSetup implements Setup {

	public void init(NutConfig conf) {
		Ioc ioc = conf.getIoc();
		Dao dao = ioc.get(Dao.class);
		Daos.createTablesInPackage(dao, "com.dating", false);
		// 初始化数据
		initACL(ioc);

	}

	public void destroy(NutConfig conf) {

	}

	/**
	 * 
	 * initACL:初始化访问控制. <br/>
	 *
	 * @author tovish
	 * @param ioc
	 */
	private void initACL(Ioc ioc) {

		// 初始化管理员
		UserDao userDao = ioc.get(UserDao.class);
		User superMan = null;
		if ((superMan = userDao
				.findByCondition(Cnd.where("name", "=", "admin"))) == null) {
			superMan = new User();
			superMan.setEmail("tovishlee@163.com");
			superMan.setName("admin");
			superMan.setPassword(Lang.md5("123456" + ByteSource.Util.bytes(superMan.getName())));
			superMan.setPhone("18996359755");
			superMan.setRealName("tovish");
			superMan.setStatus(Status.ACTIVED);
			superMan = userDao.save(superMan);
		}

		RoleDao roleDao = ioc.get(RoleDao.class);
		PermissionDao permissionDao = ioc.get(PermissionDao.class);
		UserRoleDao userRoleDao = ioc.get(UserRoleDao.class);
		RolePermissionDao rpDao = ioc.get(RolePermissionDao.class);

		// 初始化内置管理员角色
		Role admin = roleDao.findByCondition(Cnd.where("name", "=", "admin"));
		if (admin == null) {
			admin = roleDao.save(InstalledRole.ADMIN.toRole());
		}

		// 设置超级管理员
		if (userRoleDao.findByCondition(Cnd.where("userId", "=",
				superMan.getId()).and("roleId", "=", admin.getId())) == null) {
			UserRole userRole = new UserRole();
			userRole.setUserId(superMan.getId());
			userRole.setRoleId(admin.getId());
			userRole.setType(superMan.getUserType());
			userRoleDao.save(userRole);
		}

		// 初始化权限
		for (InstalledPermission permission : InstalledPermission.values()) {
			Permission p = permissionDao.findByCondition(Cnd.where("name", "=",
					permission.getName()));

			// 没有找到权限，则添加
			if (p == null) {
				p = permissionDao.save(permission.toPermission());
			}

			// 为admin授权
			if (rpDao.findByCondition(Cnd.where("roleId", "=", admin.getId())
					.and("permissionId", "=", p.getId())) == null) {
				RolePermission rp = new RolePermission();
				rp.setRoleId(admin.getId());
				rp.setPermissionId(p.getId());
				rpDao.save(rp);
			}

		}
	}

}